package day05;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/14
 */

import java.util.Arrays;

/**
 * String
 * 1.引用数据类型
 * 2.字符串的数据类型
 * 3.默认值null
 * 4.不可变字符串  public final class String
 * 5.String的创建方式
 * 方式一：类似于基本数据类型，直接赋值   "字符串常量池"
 * String str = "abc";
 * 方式二：通过构造方法创建对象  "堆内存"
 * String str = new String("abc");
 */

public class StringDemo {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = new String("abc");
        System.out.println(s1 == s2);

        /**
         * String <----> 数组 之间的转换
         * 1.已知数组，转String类型
         * 1.1 public String(byte bytes[])
         * 1.2 public String(byte bytes[], String charsetName)
         * 1.3 public String(char value[])
         */
        byte[] b = {97,98,99,100,101,102};
        char[] c = {'a','b','c','d','e'};
        String s3 = new String(b);
        System.out.println(s3);
        String s4 = new String(c);
        System.out.println(s4);

        /**
         * 2.已知String类型，转数组
         * 2.1 public byte[] getBytes()
         * 2.2 public byte[] getBytes(String charsetName)
         * 2.3 public char[] toCharArray()
         * 2.4 public String[] split(String regex)
         */
        byte[] bytes = s1.getBytes();
        System.out.println(Arrays.toString(bytes));
        String s5 = "hello";
        char[] charArray = s5.toCharArray();
        System.out.println(Arrays.toString(charArray));
        String s6 = "YZH@yzh@com";
        String[] split = s6.split("@");
        System.out.println(Arrays.toString(split));
    }
}
